import Vue from 'vue'
import Vuex from 'vuex'
import menuNav from './menuNav'
import menuListItem from './menuListItem'
import menuFooterBanner from './menuFooterBanner'
import shoppingListItem from './shoppingListItem'
import shoppingFooterBanner from './shoppingFooterBanner'
import orderListItem from './orderListItem'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        detailList: [],
        menuList: []
    },
    getters: {
    },
    actions: {
        async getDetailList({commit}) {
            await axios.get('/static.json').then(res => {
                commit('SetDetailList', res.data.detailList)
            })
        },
        async getMenuList({commit}) {
            await axios.get('./static.json').then(res => {
                commit('SetMenuList', res.data.menuList)
            })
        }
    },
    mutations: {
        calculateCount(state, data) {
            state.detailList.forEach(item => {
                if(item.id == data.item.id) {
                    item.count = data.flag ? item.count+1 : item.count-1;
                }
            })
        },
        SetDetailList(state, data) {
            state.detailList = data
        },
        AddDetailList(state, data) {
            // 方法一
            // state.detailList.push(data)
            // 方法二
            state.detailList = state.detailList.concat(data)
        },
        SetMenuList(state, data) {
            state.menuList = data
        },
    },
    // 模块化暂未实现
    modules: {
        menuNav,
        menuListItem,
        menuFooterBanner,
        shoppingListItem,
        shoppingFooterBanner,
        orderListItem
    }
})